/**
 * 
 */
package com.tvshows.front.listener.dialog;

import java.io.File;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.hibernate.exception.JDBCConnectionException;

import com.tvshows.front.ui.dialog.DialogUI;
import com.tvshows.front.view.dialog.DialogAddFavoriteView;
import com.tvshows.front.view.dialog.DialogOptionsView;
import com.tvshows.interactions.ObserverBdd;
import com.tvshows.structure.bdd.BddQueries;
import com.tvshows.structure.log.LoggingTreatment;

/**
 * @author Tangi Midy
 */
public class DialogListener
    implements Listener
{

    /**
     * The dialog view
     */
    private final DialogUI dialog;

    /**
     * The logger
     */
    Logger logger = Logger.getLogger( DialogListener.class );

    /**
     * Constructeur
     */
    public DialogListener( DialogUI dialog )
    {
        this.dialog = dialog;
    }

    /**
     * Disposes the dialog
     */
    private void dispose()
    {

        dialog.getShell().dispose();
    }

    /**
     * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
     */
    @Override
    public void handleEvent( Event e )
    {
        try
        {
            if ( dialog.getClass() == DialogAddFavoriteView.class
                && e.widget == ( (DialogAddFavoriteView) dialog ).getOkButton() )
            {
                updateOrAdd( ( (DialogAddFavoriteView) dialog ).isModify(), dialog );
                dispose();
            }

            else if ( dialog.getClass() == DialogAddFavoriteView.class
                && e.widget == ( (DialogAddFavoriteView) dialog ).getCancelButton() )
            {
                dispose();
            }

            /* si il s'agit de l'option Frame */
            if ( dialog.getClass() == DialogOptionsView.class )
            {
                if ( e.item == ( (DialogOptionsView) dialog ).getPropertiesTreeItem() )
                {
                    ( (DialogOptionsView) dialog ).initOptionsPanel( "PropertiesItem" );
                }

                else if ( e.item == ( (DialogOptionsView) dialog ).getAboutTreeItem() )
                {
                    ( (DialogOptionsView) dialog ).initOptionsPanel( null );
                }

                else if ( e.widget == ( (DialogOptionsView) dialog ).getApplyButton() )
                {
                    ( (DialogOptionsView) dialog ).applyChanges();
                    dispose();
                }

                else if ( e.widget == ( (DialogOptionsView) dialog ).getBrowseButton() )
                {
                    browseDialog( dialog, false, false );
                }

                else if ( e.widget == ( (DialogOptionsView) dialog ).getBrowseButtonHibernate() )
                {
                    browseDialog( dialog, true, false );
                }

                else if ( e.widget == ( (DialogOptionsView) dialog ).getBrowsePropertiesButton() )
                {
                    browseDialog( dialog, false, true );
                }

                else if ( e.widget == ( (DialogOptionsView) dialog ).getCancelButton() )
                {
                    dispose();
                }
            }
        }
        catch ( JDBCConnectionException exception )
        {
            exception.printStackTrace();
            LoggingTreatment.getInstance().logError( logger, "Une erreur BDD est survenue " + exception.getMessage() );
            dialog.getShell().dispose();
        }
    }

    /**
     * Opens the browsing dialog
     * 
     * @param dialog the dialog view
     * @param isHibernate if isPathHibernate
     * @param isProperties if isPathProperties
     */
    private void browseDialog( DialogUI dialog, boolean isHibernate, boolean isProperties )
    {
        FileDialog fDialog = new FileDialog( dialog.getShell(), SWT.NULL );
        String path = fDialog.open();
        if ( path != null && new File( path ).isFile() )
        {
            if ( !isHibernate && !isProperties )
            {
                ( (DialogOptionsView) dialog ).setSoftPath( path );
            }
            else if ( isHibernate && path.endsWith( ".cfg.xml" ) )
            {
                ( (DialogOptionsView) dialog ).setHibernatePath( path );
            }
            else if ( isProperties && path.endsWith( ".properties" ) )
            {
                ( (DialogOptionsView) dialog ).setPropertiesPath( path );
            }
        }
    }

    /**
     * Updates or Adds a tvshow
     * 
     * @param modify is modify
     * @param dialog the dialog view
     */
    private void updateOrAdd( boolean modify, DialogUI dialog )
    {
        String[] params = ( (DialogAddFavoriteView) dialog ).getTexts();
        if ( ( (DialogAddFavoriteView) dialog ).isModify() )
        {
            BddQueries.getBddQueries().updateFavorite( params[0], params[1], params[2] );
            ObserverBdd.getObserverBdd().reloadListFavorite();
        }
        else
        {
            BddQueries.getBddQueries().addFavorite( params[0], params[1], params[2] );
            ObserverBdd.getObserverBdd().reloadListFavorite();
        }

    }
}
